#include "form_home.h"
#include "ui_form_home.h"

// add necessary includes here
#include "ui/home/homepage/form_testing.h"


/*
 * Function:    FormHome
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormHome::FormHome(QWidget *parent) :
    QWidget(parent)
  , ui(new Ui::FormHome)
{
    qDebug() << Q_FUNC_INFO;
    ui->setupUi(this);
}

/*
 * Function:    ~FormHome
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormHome::~FormHome()
{
    qDebug() << Q_FUNC_INFO;
    delete ui;

    if(m_pFrmTesting) {
        delete m_pFrmTesting;
        m_pFrmTesting = nullptr;
    }
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormHome" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormHome" class
 */
FormHome* FormHome::getInstance(QWidget *pParent)
{
    static FormHome *pNew = nullptr;
    if(!pNew) {
        // Create a new object if it doesn't exist
        pNew = new FormHome(pParent);
        if(nullptr == pNew) return nullptr;

        // After constructing object, initialize the object
        pNew->_initialze();

        // Init the window style
        pNew->_initUi();
    }

    // Set the parent object
    if(pParent) pNew->setParent(pParent);

    return pNew;
}

/*
 * Function:    _initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormHome::_initialze()
{
    // Init an object
    if(!m_pFrmTesting) m_pFrmTesting = FormTesting::getInstance(this);
    Q_ASSERT(m_pFrmTesting);

    m_pFrmTesting->setFixedSize(DEF_MAINFRAME_WIDTH, DEF_MAIN_WIDGET_HEIGHT);
    ui->formLayout_Testing->addWidget(m_pFrmTesting);

    // Signal-slot initialization
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI()), this, SLOT(slotRefreshUI()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    vecConnect.push_back(QObject::connect(m_pFrmTesting, SIGNAL(showShowPageResultDetails(int)), this, SIGNAL(showShowPageResultDetails(int)), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormHome::_initUi()
{
    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/home/homepage/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->stackedWidget->setCurrentIndex(1);

    emit m_pFrmTesting->signalRefreshUI();
}

/*
* Function:		slotRefreshUI
* Description:	The slot that refresh UI info
* Paramer:		void
* Return:		void
*/
void FormHome::slotRefreshUI()
{
    qDebug() << Q_FUNC_INFO;

    emit m_pFrmTesting->signalRefreshUI(false);
    emit m_pFrmTesting->signalRefreshProcedure();
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *e - the pointer of "QEvent"
* Return:		void
*/
void FormHome::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
    {
        qDebug() << Q_FUNC_INFO;
        ui->retranslateUi(this);

        break;
    }
    default:
        break;
    }

    QWidget::changeEvent(e);
}
